package com.yitong.base.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
import java.text.ParseException;

/**
 * Utils_时间工具类
 * 
 * @author kuaiditu
 * @version 1.0
 */
public class DateUtils {
	
	/**
	 * 判断是否分配快递员
	 * @return
	 */
	public static boolean ispushmessage(){
		
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int minu = Calendar.getInstance().get(Calendar.MINUTE);
		if(hour>8 && hour<17){
			return true;
		}
		if(hour == 17){
			if(minu<30){
				return true;
			}
		}
		return false;
	}

	// 根据当前时间生成基础单号
	public static String getBaseOrderNo() {
		// 字符化时间戳
		String time = String.valueOf(new Date().getTime());
		String no = time.substring(0, time.length() - 1);
		return no;
	}
	
	//当前时间是否早于特定时间(2012-12-12 12:12:12)
	public static boolean isbefore(String time){
		try{
			//时间格式化
			if(time.length()<10){
				time = Calendar.getInstance().get(Calendar.YEAR)+"-"+(Calendar.getInstance().get(Calendar.MONTH)+1)+"-"+Calendar.getInstance().get(Calendar.DATE)+" "+time;
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
			//格式化时间
			Date before = format.parse(time);
			return new Date().before(before);
		}catch(Exception ex){
			ex.getMessage();
			return false;
		}
	}
	
	//比较一个时间跟当前时间的差值(按天计算)
	public static int differDayDefault(String datetime){
		//日历类
		try{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
			//当前时间毫秒数
			long curr = System.currentTimeMillis();
			Date date = format.parse(datetime);
			long datemillis = date.getTime();
			long differ = curr>datemillis?curr-datemillis:datemillis-curr;
			return (int)(differ/(1000*60*60*24));
		}catch(Exception ex){
			ex.printStackTrace();
			return 0;
		}
	}
	
	// 生成礼券规则
		public static String getGiftOrderCode() throws Exception {
			Date date = new Date();
			String inputStr = "WXKDT" + new SimpleDateFormat("yyyyMMddhhmmssSSS").format(date)+UUID.randomUUID();
			byte[] inputData = inputStr.getBytes();
			BigInteger sha = new BigInteger(DateUtils.encryptSHA(inputData));
			return "WX" + sha.toString(32).toUpperCase();
		}
		
	// WxNo订单编号2015-03-12 09:36:51
	public static String getStrWXNo(String orderNo) throws Exception {
	
		return System.currentTimeMillis()+"KDT"+orderNo;
	}
		
		public static void main(String[] args) throws Exception {
			for (int i = 0; i < 100; i++) {
				System.out.println(getGiftOrderCode());
			}
		}
		
		private static final String KEY_SHA = "SHA";

		public static byte[] encryptSHA(byte[] data) throws Exception {
			MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
			sha.update(data);
			return sha.digest();
		}

		/**
		 * 生成当前时间
		 * 
		 * @return
		 */
		public static String GetNowDate() {
			Date dt = new Date();
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dt);
		}

		/**
		 * 判断是否在有效期类 1.电话号码不相同,发
		 * 
		 * @throws ParseException
		 * 
		 * 
		 */
		private static final long time_interval = 56;

		public static boolean isVerification(String Str, String moblie) throws ParseException {

			boolean temp = false;
			String mb = Str.substring(5, Str.indexOf("验"));
			//String vd = Str.substring(Str.indexOf("码:") + 2, Str.indexOf("时"));
			String da = Str.substring(Str.indexOf("间:") + 2, Str.length());
			/**
			 * 电话号码相同,比较时间
			 */
			if (mb.equals(moblie)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date d1 = sdf.parse(da); // 存进来的值
				Date d2 = sdf.parse(sdf.format(new Date()));
				long diff = d2.getTime() - d1.getTime();
				diff = diff / 1000;
				if (diff > time_interval) {
					temp = true;
				} else {
					temp = false;
				}
			} else {
				temp = true;
			}
			return temp;
		}
		// 记录日志信息
		@SuppressWarnings("deprecation")
		public static void write(String content, String filename) {

			// 日期
			Date d = new Date();
			// 拼接文件名称：日期+标识
			String path = "/data/log/" + (d.getYear() - 100) + "-" + (d.getMonth() + 1) + "-" + d.getDate();
			// wenjian
			String filepath = "/data/log/" + (d.getYear() - 100) + "-" + (d.getMonth() + 1) + "-" + d.getDate() + "/" + filename;
			File file = new File(path);
			if (!file.exists() && !file.isDirectory()) {
				file.mkdirs();
			}
			// String path = "F:\\"+filename;
			try {
				// 文件
				File f = new File(filepath);
				// 判断文件文件是否存在
				if (!f.exists()) {
					// 创建文件
					f.createNewFile();
				}
				// 拼接内容
				StringBuffer sb = new StringBuffer("*************************\r\n");
				sb.append(new Date() + " " + content + "\r\n");
				// 写入流
				FileWriter fw = new FileWriter(filepath, true);
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write(sb.toString());
				// 关闭流
				bw.close();
				fw.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
}
